package org.niux.AutoLoginQQ.operate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.niux.AutoLoginQQ.common.Constant;

/**
 * 
 * Copyright (c) 2012,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：AppOperat.java<br>
 * 摘 要：软件设置<br>
 * <br>
 * 当前版本：<br>
 * 作 者：mingxian Email: <a
 * href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2012-10-23<br>
 * <br>
 * 取代版本：<br>
 * 原作者 ：<br>
 * 完成日期：@since 2012-10-23<br>
 */
public class AppOperate {
	private Log log = LogFactory.getLog(this.getClass());

	public void saveAppInfo() {
		// 查询注册表中是否已存在启动项
		try {
			Process p = Runtime
					.getRuntime()
					.exec("reg query HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v niuxqq");
			
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setAutoRun(boolean autoRun) {
		// 设置开机自启动
		String path = System.getProperty("user.dir");

		try {
			// 获取程序名
			String processName = java.lang.management.ManagementFactory.getRuntimeMXBean()
					.getName();

			long pid = Long.parseLong(processName.split("@")[0]);

			String[] args = new String[] { "cmd.exe", "/c", "tasklist|findstr", Long.toString(pid) };

			Process process = Runtime.getRuntime().exec(args);

			InputStreamReader in = new InputStreamReader(process.getInputStream(), "GBK");
			BufferedReader reader = new BufferedReader(in);

			String taskInfo = null;

			String prcessname = null;

			while ((taskInfo = reader.readLine()) != null) {
				if (taskInfo.trim().length() > 0 && taskInfo.toUpperCase().indexOf("K") != -1) {
					String[] strs = taskInfo.split("\\s");
					prcessname = strs[0];
				}
			}

			path = path + "\\" + prcessname + " " + Constant.VERSION + ".exe";

			log.debug("软件路径：" + path);

		} catch (IOException e1) {
			e1.printStackTrace();
		}

		try {
			// 查询注册表中是否已存在启动项
			Process p = Runtime
					.getRuntime()
					.exec("reg query HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v niuxqq");
			p.waitFor();

			if (autoRun) { // 软件自启动
				log.debug("软件设置为自启动");
				// 没有启动项
				if (p.exitValue() == 1) {
					log.debug("没有启动项");
					// 添加启动项
					p = Runtime.getRuntime().exec(
							"reg add \"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\"
									+ "Run\" /v niuxqq /t REG_SZ /d \"" + path + "\"");
					p.waitFor();
					log.debug(p.exitValue());
				} else { // 存在启动项
					log.debug("存在启动项，进行修改");
					// 删除旧的项
					p = Runtime
							.getRuntime()
							.exec("reg delete HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /f /v niuxqq");
					p.waitFor();
					// 添加新启动项
					p = Runtime.getRuntime().exec(
							"reg add \"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\"
									+ "Run\" /v niuxqq /t REG_SZ /d \"" + path + "\"");
					p.waitFor();
				}
			} else { // 软件不自启动
				log.debug("软件设置为不自启动");
				// 没有启动项
				if (p.exitValue() != 1) {
					log.debug("存在启动项，进行修改");
					// 删除启动项
					p = Runtime
							.getRuntime()
							.exec("reg delete HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /f /v niuxqq");
					p.waitFor();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
